package com.example.bookshop.service.user;

import com.example.bookshop.entity.*;
import com.example.bookshop.repository.BusinessRepository;
import com.example.bookshop.repository.CustomerRepository;
import com.example.bookshop.repository.GoodsRepository;
import com.example.bookshop.repository.OrderRepository;
import com.example.bookshop.result.Result;
import com.example.bookshop.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    OrderRepository orderRepository;
    @Autowired
    CustomerRepository customerRepository;
    @Autowired
    GoodsRepository goodsRepository;
    @Autowired
    private BusinessRepository businessRepository;

    @Override
    public Result addOrder(Order order, HttpSession session) {
        try {
            MyUtil myUtil = new MyUtil();
            Customer customer = customerRepository.findCustomerById(myUtil.getUserId(session));
            Integer goodsId = order.getGoodsId();
            order.setCustomerId(customer.getId());
            Goods goods=goodsRepository.findGoodsById(goodsId);
            order.setBusinessId(goods.getBusinessId());
            order.setGoodsId(goodsId);
            order.setTime(myUtil.getStringID());
            order.setGoodsName(goods.getGoodsName());
            order.setPhoto(goods.getPhoto());
            order.setPrice(order.getNumber()*goods.getPrice());
            order.setState("1");

            Customer c1=customerRepository.findCustomerById(order.getCustomerId());
            Customer c2=customerRepository.findCustomerById(businessRepository.findBusinessById(goods.getBusinessId()).getCustomerId());
            if(c1.getId()!=c2.getId()){
                if(c1.getMoney()>=order.getPrice()){//判断用户A的余额是否大于购买订单金额
                    float money= c1.getMoney()-order.getPrice();
                    c1.setMoney(money);//将用户余额减少
                    customerRepository.updateCustomer(c1);
                    orderRepository.addOrder(order);
                    int m=goods.getNumber()-1;
                    goods.setNumber(m);
                    goodsRepository.updateGoods(goods);
                    return new Result(200);
                }
                else return new Result(400);
            }
            else return new Result(400);

        }catch (Exception e){
            return new Result(500);
        }
    }

    @Override
    public List<BaseOrder> getListOrder(HttpSession session) {
        MyUtil myUtil=new MyUtil();
        Business business=businessRepository.findBusinessByUserId(myUtil.getUserId(session));
        return orderRepository.findOrderByBusinessId(business.getId());
    }

    @Override
    public Order OrderDetail(Integer id) {
        return orderRepository.findOrderById(id);
    }

    @Override
    public Result deliverGoods(Order order) {
        Order myOrder=orderRepository.findOrderById(order.getId());
        myOrder.setSummary(order.getSummary());
        myOrder.setState("2");
        Integer i=orderRepository.updateOrder(myOrder);
        if(i==1){
            return new Result(200);
        }
        else return new Result(400);
    }

    @Override
    public List<BaseOrder> getUserOrder(HttpSession session) {
        MyUtil myUtil=new MyUtil();
        return orderRepository.findOrderByCustomerId(myUtil.getUserId(session));
    }

    @Override
    public Result confirmOrder(Integer id) {
        Order order=orderRepository.findOrderById(id);
        Integer businessId=order.getBusinessId();
        Business business=businessRepository.findBusinessById(businessId);
        Customer customer=customerRepository.findCustomerById(business.getCustomerId());//查询出用户的id
        float money=customer.getMoney()+order.getPrice();
        customer.setMoney(money);
        Integer m=customerRepository.updateCustomer(customer);//给商家的余额添加金额
        order.setState("3");
        Integer n=orderRepository.updateOrder(order);//将状态该为已签收
        if(n==1&&m==1){
            return new Result(200);
        }
        else return new Result(400);
    }
}
